package me.lc.linklinkchrist.sprite;

import me.lc.linklinkchrist.R;
import me.lc.linklinkchrist.core.GameSystem;
import me.lc.linklinkchrist.core.StageManager;
import me.lc.linklinkchrist.entity.StageInfo;
import me.lc.linklinkchrist.scene.GameScene;
import me.lc.linklinkchrist.util.ADHelper;
import me.lc.linklinkchrist.util.AudioHelper;
import me.lc.linklinkchrist.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;

/**
 * 关卡按钮Node
 * 
 * @author Administrator
 * 
 */
public class StageButtonNode extends Node {
	Button button;
	Button button1;
	Button button2;
	Button button3;
	Label label;
	Sprite locker;
	int stageIndex;
	boolean isLocked = false;

	public StageButtonNode(int index) {
		stageIndex = index - 1;
		StageInfo info = StageManager.instance.stageInfos.get(stageIndex);
		button = Button.make(R.drawable.stagebutton,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		addChild(button);

		// star btn
		button1 = Button.make(R.drawable.stagebutton_star1,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		button1.setVisible(false);
		addChild(button1);
		button2 = Button.make(R.drawable.stagebutton_star2,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		button2.setVisible(false);
		addChild(button2);
		button3 = Button.make(R.drawable.stagebutton_star3,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		button3.setVisible(false);
		addChild(button3);

		label = Label.make(index + "", 17);
		addChild(label);
		locker = Sprite.make(GameSystem.stageLockerTex);
		addChild(locker);
		locker.setVisible(false);

		if (info.isLock()) {
			isLocked = true;
			locker.setVisible(true);
			label.setVisible(false);
		} else {
			// 设置star
			setStar(info);
		}
	}

	/**
	 * 设置关卡得分star
	 * 
	 * @param info
	 */
	private void setStar(StageInfo info) {
		if (info.getScore() > 0) {
			if (info.getScore() >= GameSystem.Star3Score) {
				// 3星
				button.setVisible(false);
				button1.setVisible(false);
				button2.setVisible(false);
				button3.setVisible(true);
			} else if (info.getScore() >= GameSystem.Star2Score) {
				// 2星
				button.setVisible(false);
				button1.setVisible(false);
				button2.setVisible(true);
				button3.setVisible(false);
			} else {
				// 1星
				button.setVisible(false);
				button1.setVisible(true);
				button2.setVisible(false);
				button3.setVisible(false);
			}
		} else {
			button.setVisible(true);
			button1.setVisible(false);
			button2.setVisible(false);
			button3.setVisible(false);
		}

	}

	public void refresh() {
		StageInfo info = StageManager.instance.stageInfos.get(stageIndex);
		if (info.isLock()) {
			isLocked = true;
			locker.setVisible(true);
			label.setVisible(false);
		} else {
			isLocked = false;
			locker.setVisible(false);
			label.setVisible(true);
			setStar(info);
		}
	}

	public void onButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				if (!isLocked) {
					AudioHelper.clickItem();
					StageManager.instance.currentStage = stageIndex;
					GameScene.make().startNewGame();
					GameUtil.switchSceneFlipX(GameScene.make());
					ADHelper.showAd();
				}
			}
		});
	}
}
